﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WcfServiceLibrary;
using System.Net;
using System.Xml.Serialization;
using System.Collections;
namespace _1041326_1041339_1041433.UserPages
{
    public partial class Reader : System.Web.UI.Page
    {
        String url = "http://localhost:1871/Service.svc";
        private List<CustomSyndicationItem> _rssItems;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Email"] == null)
            {
                Response.Redirect("~/UserPages/Login.aspx");
                return;
            }            
            
            Raise_Msg(string.Empty);
            HienThi();
        }

        private void HienThi()// chưa load dc của user
        {
            if (Session["Username"] != null)
            {
                WebRequest wr = WebRequest.Create(url + "/dsRss/" + Session["Username"]);
                WebResponse wp = wr.GetResponse();
                XmlSerializer serial = new XmlSerializer(typeof(ArrayOfRssDTO));
                ArrayOfRssDTO ds = (ArrayOfRssDTO)serial.Deserialize(wp.GetResponseStream());

                gvMenu.DataSource = ds.Items;
                gvMenu.DataBind();
                String link = ds.Items[0].Link;

                if (Request["id"] != null)
                {
                    link = Request["id"];
                }
                try
                {
                    List<CustomSyndicationItem> multiRss = RssHelper.Get_RSS(link);
                    this.gvRssList.DataSource = multiRss;
                    this.gvRssList.DataBind();
                }
                catch (Exception ex)
                {
                    Raise_Msg(ex.Message);
                }
            }
        }

        protected void Btn_Subcribe_Onclick(object sender, EventArgs e)
        {

            HienThi();
        }
        // Fire message to user
        protected void Raise_Msg(string msg)
        {
            lbMessage.Text = msg;
            this.quickadd.Text = string.Empty;
        }

        protected void gvRssList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvRssList.PageIndex = e.NewPageIndex;
        }

    }
}
